#!/usr/bin/env python
#coding:utf-8
#
# Copyright 2010 Close To U.
#
# Author:Darcy<http://closetou.com>
#

from google.appengine.ext import db
#from google.appengine.api import memcache

class Accumulator(db.Model):
  name = db.StringProperty()
  counter = db.IntegerProperty(required=True, default=0)

class Options(db.Model):
  """The setting option model."""
  option_name = db.StringProperty()
  option_value = db.StringProperty()
  autoload = db.BooleanProperty(default=True)
  def get(self, name):
    q = db.GqlQuery('SELECT * FROM Options WHERE option_name = :1',name)
    if q.count() == 1:
      value = q[0].option_value
      return value
    else:
      return None
  get = classmethod(get)

  def set(self,name,value):
    q = db.GqlQuery('SELECT * FROM Options WHERE option_name = :1',name)
    if q.count() == 1:
      d = q[0]
    else:
      d = Options()
      d.option_name = name
    d.option_value = value
    d.put()
  set = classmethod(set)

##class Members(db.Model):
##  member_id = db.IntegerProperty()
##  username = db.StringProperty()
##  nickname = db.StringProperty()
##  email = db.EmailProperty()
##  password = db.StringProperty()
##  avatar = db.BlobProperty()
##  created_at = db.DateTimeProperty(auto_now_add=True)
##  updated_at = db.DateTimeProperty(auto_now=True)
##  deleted = db.BooleanProperty(default=False)
##  member = db.ReferenceProperty(Members)

class Blobs(db.Model):
  blob_id = db.IntegerProperty()
  content_type = db.StringProperty()
  filename = db.StringProperty()
  size = db.IntegerProperty(default=0)
  hash_value = db.StringProperty()
  blob = db.BlobProperty()
  view_count = db.IntegerProperty(default=0)
  created_at = db.DateTimeProperty(auto_now_add=True)
  updated_at = db.DateTimeProperty(auto_now=True)
  deleted = db.BooleanProperty(default=False)
  def getbyhash(self, hash_value):
    q = db.GqlQuery('SELECT * FROM Blobs WHERE hash_value = :1',hash_value)
    if q.count() == 1:
      return q[0]
    else:
      return None
  getbyhash = classmethod(getbyhash)
  
  def getbyid(self, blob_id):
    q = db.GqlQuery('SELECT * FROM Blobs WHERE blob_id = :1',int(blob_id))
    if q.count() == 1:
      return q[0]
    else:
      return None
  getbyid = classmethod(getbyid)
  
  
